package com.cgsd.heyu.utils;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;

import com.apkfuns.logutils.LogUtils;
import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;
import com.cgsd.heyu.api.AppConfig;

import java.util.List;

/**
 * author:
 * time:2021/4/19
 * description:
 */
public class AdUtils {


    public static void loadVideoAd(Context mContext, FrameLayout mAdContainer, String codeId, IAdStatusCallBack callBack) {

        if (mContext == null || mAdContainer == null || TextUtils.isEmpty(codeId)) return;

        TTAdNative mTTAdNative = TTAdSdk.getAdManager().createAdNative(mContext);
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(codeId) //广告位id
                .setSupportDeepLink(true)
                .setAdCount(1) //请求广告数量为1到3条
                .setExpressViewAcceptedSize(600, 0) //期望模板广告view的size,单位dp
                    .setUserID(AppConfig.AD_USER_ID)
                .build();

        mTTAdNative.loadExpressDrawFeedAd(adSlot, new TTAdNative.NativeExpressAdListener() {
            //广告请求失败
            @Override
            public void onError(int code, String message) {
                com.apkfuns.logutils.LogUtils.e("onError" + message);
                mAdContainer.removeAllViews();
                if (callBack != null) {
                    callBack.onRenderFail(null, message, code);
                }
            }

            //广告请求成功
            @Override
            public void onNativeExpressAdLoad(List<TTNativeExpressAd> ads) {
                com.apkfuns.logutils.LogUtils.e("加载成功 size=" + ads.size());

                TTNativeExpressAd mTTAd = ads.get(0);

                mTTAd.setVideoAdListener(new TTNativeExpressAd.ExpressVideoAdListener() {
                    @Override
                    public void onVideoLoad() {
                        LogUtils.e("onVideoLoad");
                    }

                    @Override
                    public void onVideoError(int i, int i1) {
                        LogUtils.e("onVideoError"+i+",i1 = "+i1);
                    }

                    @Override
                    public void onVideoAdStartPlay() {
                        LogUtils.e("onVideoAdStartPlay");
                    }

                    @Override
                    public void onVideoAdPaused() {
                        LogUtils.e("onVideoAdPaused");
                    }

                    @Override
                    public void onVideoAdContinuePlay() {
                        LogUtils.e("onVideoAdContinuePlay");
                    }

                    @Override
                    public void onProgressUpdate(long l, long l1) {
                        LogUtils.e("onProgressUpdate"+l+",l1 = "+l1);
                    }

                    @Override
                    public void onVideoAdComplete() {
                        LogUtils.e("onVideoAdComplete");
                    }

                    @Override
                    public void onClickRetry() {
                        LogUtils.e("onClickRetry");
                    }
                });
                mTTAd.setCanInterruptVideoPlay(true);
                setAdListener(mTTAd, mAdContainer, callBack);


                //调用render开始渲染广告
                mTTAd.render();
            }

        });
    }

    /**
     * 设置动画 监听
     *
     * @param mTTAd        ad
     * @param mAdContainer view
     */
    private static void setAdListener(TTNativeExpressAd mTTAd, FrameLayout mAdContainer, IAdStatusCallBack callBack) {

        mTTAd.setExpressInteractionListener(new TTNativeExpressAd.ExpressAdInteractionListener() {
            @Override
            public void onAdClicked(View view, int type) {
                com.apkfuns.logutils.LogUtils.e("onAdClicked");
                if (callBack != null) {
                    callBack.onAdClick(view, type);
                }
            }

            @Override
            public void onAdShow(View view, int type) {
                if (callBack != null) {
                    callBack.onAdShow(view, type);
                }
            }

            @Override
            public void onRenderFail(View view, String msg, int code) {
                if (callBack != null) {
                    callBack.onRenderFail(view, msg,code);
                }
            }

            @Override
            public void onRenderSuccess(View view, float width, float height) {
                if (callBack != null) {
                    callBack.onRenderSuccess(view, width,height);
                }
            }
        });


    }


    /**
     * ad callBack
     */
    public interface IAdStatusCallBack {

        void onRendLoad(List<TTNativeExpressAd> ads);

        /**
         * 点击
         *
         * @param view v
         * @param type t
         */
        void onAdClick(View view, int type);

        /**
         * 展示
         *
         * @param view v
         * @param type t
         */
        void onAdShow(View view, int type);

        /**
         * 加载失败
         *
         * @param view v
         * @param msg  m
         * @param code c
         */
        void onRenderFail(View view, String msg, int code);

        /**
         * 加载成功
         *
         * @param view   v
         * @param width  w
         * @param height h
         */
        void onRenderSuccess(View view, float width, float height);
    }

}
